@(currentPage: models.Person.Page, currentSortBy: String, currentOrder: String, currentFilter: String)

@****************************************
* Helper generating navigation links    *
****************************************@
@link(newPage:Int, newSortBy:String) = @{
    
    var sortBy = currentSortBy
    var order = currentOrder
    
    if(newSortBy != null) {
        sortBy = newSortBy
        if(currentSortBy == newSortBy) {
            if(currentOrder == "asc") {
                order = "desc"
            } else {
                order = "asc"
            }
        } else {
            order = "asc"
        }
    }
    
    // Generate the link
    routes.PersonController.list(newPage, sortBy, order, currentFilter)
    
}

@**********************************
* Helper generating table headers *
***********************************@
@header(key:String, title:String) = {
    <th class="@key.replace(".","_") header @if(currentSortBy == key) @{if(currentOrder == "asc") "headerSortDown" else "headerSortUp"}">
        <a href="@link(0, key)">@title</a>
    </th>
}

@main {
    
    <h1 id="homeTitle">@Messages("persons.list.title", currentPage.getTotalRowCount)</h1>

    @if(flash.containsKey("success")) {
        <div class="alert-message warning">
            <strong>Done!</strong> @flash.get("success")
        </div>
    } 

    <div id="actions">
        
        <form action="@link(0, "name")" method="GET">
            <input type="search" id="searchbox" name="f" value="@currentFilter" placeholder="Filter by person name...">
            <input type="submit" id="searchsubmit" value="Filter by name" class="btn primary">
        </form>
        
        <a class="btn success" id="add" href="@routes.PersonController.create()">Add a new person</a>
        
    </div>
    
    @if(currentPage.getTotalRowCount == 0) {
        
        <div class="well">
            <em>Nothing to display</em>
        </div>
        
    } else {
        
        <table class="computers zebra-striped">
            <thead>
                <tr>
                    @header("name", "Person name")
                    @header("birthday", "Birthday")
                    @header("address", "Address")
                    @header("street", "Street")
                    @header("cp", "Code Postale")
                    @header("city", "City")
                </tr>
            </thead>
            <tbody>

                @for(person <- currentPage.getList) {
                    <tr>
                        <td><a href="@routes.PersonController.edit(person.id)">@person.name</a></td>
                        <td>
                            @if(person.birthday == null) {
                                <em>-</em>
                            } else {
                                @person.birthday.format("dd/MM/yyyy")
                            }
                        </td>
                        <td>
                            @if(person.address == null) {
                                <em>-</em>
                            } else {
                                @person.address
                            }
                        </td>
                        <td>
                            @if(person.street == null) {
                                <em>-</em>
                            } else {
                                @person.street
                            }
                        </td>
                        <td>
                            @if(person.cp == null) {
                                <em>-</em>
                            } else {
                                @person.cp
                            }
                        </td>
                        <td>
                            @if(person.city == null) {
                                <em>-</em>
                            } else {
                                @person.city
                            }
                        </td>
                    </tr>
                }

            </tbody>
        </table>

        <div id="pagination" class="pagination">
            <ul>
                @if(currentPage.hasPrev) {
                    <li class="prev">
                        <a href="@link(currentPage.getPageIndex - 1, null)">&larr; Previous</a>
                    </li>
                } else {
                    <li class="prev disabled">
                        <a>&larr; Previous</a>
                    </li>
                }
                <li class="current">
                    <a>Displaying @currentPage.getDisplayXtoYofZ</a>
                </li>
                @if(currentPage.hasNext) {
                    <li class="next">
                        <a href="@link(currentPage.getPageIndex + 1, null)">Next &rarr;</a>
                    </li>
                } else {
                    <li class="next disabled">
                        <a>Next &rarr;</a>
                    </li>
                }
            </ul>
        </div>
        
    }
        
}

            